package com.biosan.utils;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import org.apache.commons.dbcp.BasicDataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;

public class GetDataSource
{
    // context.getResourceAsStream("/WEB-INF/classes/init.properties");
    private static String c3p0Properties = "/c3p0.properties";
    private static String dbcproperties = "/dbcp.properties";
    private static String sqlserverproperties = "/sqlserver.properties";

    public BasicDataSource DS = null;   

    // 该类只能有一个实例
    private GetDataSource()
    {
        Properties p = new Properties();
        try
        {
            p.load(GetDataSource.class.getResourceAsStream(dbcproperties));
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName(p.getProperty("driverClass"));
        ds.setUrl(p.getProperty("jdbcUrl"));
        ds.setUsername(p.getProperty("user"));
        ds.setPassword(p.getProperty("password"));
        ds.setMaxActive(Integer.valueOf(p.getProperty("maxactive")));
        ds.setMinIdle(Integer.valueOf(p.getProperty("minIdle")));
        ds.setMaxIdle(Integer.valueOf(p.getProperty("maxidle")));
        ds.setMaxWait(Integer.valueOf(p.getProperty("maxwait")));
        ds.setRemoveAbandoned(Boolean.valueOf(p.getProperty("removeAbandoned")));
        this.DS = ds;

    } // 私有无参构造方法

    private static GetDataSource GS = null;

    public static GetDataSource getGS()
    {
        if (GS == null)
        {
            GS = new GetDataSource();
        }
        return GS;
    }

    public BasicDataSource getDS()
    {
        return DS;
    }

    public void setDS(BasicDataSource ds) throws IOException
    {
        // return ds;
        this.DS = ds;
    }

    // public static ComboPooledDataSource c3p0Sources()
    // {
    // ComboPooledDataSource ds = new ComboPooledDataSource();
    // Properties p = new Properties();
    // try
    // {
    // // static 时 必须用 classname.class 代替 this.getClass()
    // p.load(GetDataSource.class.getResourceAsStream(c3p0Properties));
    //			
    // ds.setDriverClass(p.getProperty("driverClass"));
    // ds.setJdbcUrl(p.getProperty("jdbcUrl"));
    // ds.setUser(p.getProperty("user"));
    // ds.setPassword(p.getProperty("password"));
    // ds.setMaxPoolSize(Integer.valueOf(p.getProperty("maxPoolSize")));
    // ds.setMinPoolSize(Integer.valueOf(p.getProperty("minPoolSize")));
    // ds.setCheckoutTimeout(Integer.valueOf(p
    // .getProperty("checkoutTimeout")));
    // ds.setIdleConnectionTestPeriod(Integer.valueOf(p
    // .getProperty("idleConnectionTestPeriod")));
    // ds.setAcquireRetryDelay(Integer.valueOf(p
    // .getProperty("acquireRetryDelay")));
    // ds.setMaxIdleTime(Integer.valueOf(p.getProperty("maxIdleTime")));
    // ds.setTestConnectionOnCheckin(Boolean.valueOf(p.getProperty("testConnectionOnCheckin")));
    // ds.setTestConnectionOnCheckin(Boolean.valueOf(p.getProperty("testConnectionOnCheckout")));
    // ds.setNumHelperThreads(Integer.valueOf(p.getProperty("numHelperThreads")));
    // ds.setAutoCommitOnClose(Boolean.valueOf(p.getProperty("autoCommitOnClose")));
    // ds.setAcquireIncrement(Integer.valueOf(p.getProperty("acquireIncrement")));
    // ds.setAutomaticTestTable(p.getProperty("automaticTestTable"));
    //
    // }
    //
    // catch (PropertyVetoException e)
    // {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // } catch (IOException e2)
    // {
    // // TODO Auto-generated catch block
    // e2.printStackTrace();
    // }
    // return ds;
    //		
    // }
    //	
    @SuppressWarnings("deprecation")
    public static BasicDataSource dbcpSources() throws IOException
    {

        Properties p = new Properties();
        p.load(GetDataSource.class.getResourceAsStream(dbcproperties));
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName(p.getProperty("driverClass"));
        ds.setUrl(p.getProperty("jdbcUrl"));
        ds.setUsername(p.getProperty("user"));
        ds.setPassword(p.getProperty("password"));
        ds.setMaxActive(Integer.valueOf(p.getProperty("maxactive")));
        ds.setMinIdle(Integer.valueOf(p.getProperty("minIdle")));
        ds.setMaxIdle(Integer.valueOf(p.getProperty("maxidle")));
        ds.setMaxWait(Integer.valueOf(p.getProperty("maxwait")));
        ds.setRemoveAbandoned(Boolean.valueOf(p.getProperty("removeAbandoned")));
        return ds;
    }

    public static BasicDataSource getDataSource(String conString) throws IOException
    {
        Properties p = new Properties();
        p.load(GetDataSource.class.getResourceAsStream(sqlserverproperties));
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName(p.getProperty("driverClass"));
        ds.setUrl(p.getProperty("jdbcUrl"));
        ds.setUsername(p.getProperty("user"));
        ds.setPassword(p.getProperty("password"));
        ds.setMaxActive(Integer.valueOf(p.getProperty("maxactive")));
        ds.setMinIdle(Integer.valueOf(p.getProperty("minIdle")));
        ds.setMaxIdle(Integer.valueOf(p.getProperty("maxidle")));
        ds.setMaxWait(Integer.valueOf(p.getProperty("maxwait")));
        ds.setRemoveAbandoned(Boolean.valueOf(p.getProperty("removeAbandoned")));
        return ds;
    } 


    public static Connection getDbcpConnection()
    {
        Connection connection = null;
        try
        {
            connection = dbcpSources().getConnection();
        }
        catch (SQLException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return connection;
    }

    public static Connection getConnection() throws IOException
    {
        Connection connection = null;
        Properties p = new Properties();

        p.load(GetDataSource.class.getResourceAsStream(dbcproperties));
        try
        {
            connection = DriverManager.getConnection(p.getProperty("jdbcUrl"), p.getProperty("user"), p.getProperty("password"));
            ;
        }
        catch (SQLException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return connection;
    }
    // public static Connection getC3p0Conn()
    // {
    // Connection connection=null;
    // try
    // {
    // connection= c3p0Sources().getConnection();
    // } catch (SQLException e)
    // {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // }
    // return connection;
    // }

}
